package com.lgc.provider01.config;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 生产者产生消息代码
 * @author lvguangchun
 * @create 2022-08-30 9:46
 */

public class ProviderConfig {
    public static void main(String[] args) throws Exception {
        //实例化消息生产者Producer对象
        DefaultMQProducer producer = new DefaultMQProducer("provider");
        //设置NameServer地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //启动Producer实例
        producer.start();
        for (int i=0;i<10;i++){
            //创建消息，并指定Topic、Tag和消息体
            Message msg = new Message("producer","TagA",("Hello MQ!"+i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            //Producer实例发送消息
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n",sendResult);
        }
        //关闭Producer实例
        producer.shutdown();
    }
}
